#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include "Log.hpp"
#include "thread.hpp"
#include "Cond.hpp"
#include "Mutex.hpp"

namespace ThreadPoolModule
{
    using namespace MutexModule;
    using namespace CondModule;
    using namespace ThreadModule;
    using namespace LogModule;

    static const int gnum = 5;

    template <typename T>
    class ThreadPool
    {
    private:
        void WakeUpAllThread()
        {
            LockGuard lockguard(_mutex);
            if(_sleepernum)
                _cond.BroadCast();
            Log(Level::INFO) << "唤醒所有线程";
        }

    void WakeUpOne()
    {
        _cond.Signal();
        LOG(Level::INFO) << "唤醒一个休眠线程";
    }

    ThreadPool(int num = gnum) : _num(num), _isrunning(false), _sleepernum(0)
    {
        for(int i = 0; i < _num; i++)
        {
            _thread.emplace_back(
                [this]()
                {
                    HandlerTask();
                }
            );
        }
    }

    void Start()
    {
        if(_isrunning)
            return;
        _isrunning = true;
        for(auto& thread : _threads)
        {
            thread.Start();
            LOG(Level::INFO) << "start new thread success: " << thread.Name(); 
        }
    }

    ThreadPool(const ThreadPool<T>&) = delete;
    ThreadPool<T>& operator=(const ThreadPool<T>&) = delete;

    public:
        static ThreadPool<T>* GetInstance()
        {
            if(inc == nullptr)
            {
                LockGuard lockguard(_lock);
                LOG(Level::DEBUG) << "获取单例...";
                if(inc == nullptr)
                {
                    LOG(Level::DEBUG) << "首次使用单例，创建之...";
                    inc = new ThreadPool<T>();
                    inc->Start();
                }

            }

            return inc;
        }

        void Stop()
        {
            if(!_isrunning)
                return;
            _isrunning = false;
            WakeUpAllThread();
        }

        void Join()
        {
            for(auto& thread : _threads)
            {
                thread.Join();
            }
        }

        void HandlerTask()
        {
            char name[128];
            pthread_getname_np(pthread_self(), name, sizeof(name));
            while (true)
            {
                T t;
                {
                    LockGuard lockguard(_mutex);
                    while(_taskq.empty() && _isrunning)
                    {
                        _sleepernum++;
                        _cond.Wait();
                        _sleepernum--;
                    }

                    if(!_isrunning && _taskq.empty())
                    {
                        LOG(Level::INFO) << name << "退出了， 线程池退出&&任务队列为空";
                        break;
                    }


                    t = _taskq.front();
                    _taskq.pop();
                }
                t();
            }
            
        }

        bool Enqueue(const T& in)
        {
            if(_isrunning)
            {
                LockGuard lockguard(_mutex);
                _taskq.push(in);
                if(_threads.size() == _sleepernum)
                    WakeUpOne();
                return true;
            }

            return false;
        }

        ~ThreaedPool()
        {}

    private:
        std::vector<Thread> _threads;
        int _num;
        std::queue<T> _taskq;
        Cond _cond;
        Mutex _mutex;

        bool _isrunning;
        int _sleepernum;

        static ThreadPool<T>* inc;
        static Mutex _lock;
    };

    template <typename T>
    ThreadPool<T>* ThreadPool<T>::inc = nullptr;

    template <typename T>
    MutexModule::Mutex ThreadPool<T>::_lock;



}